package com.xinchen.aiowaste.vm

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.maple.model.data.AioUserInfo
import com.maple.model.entity.aio.DevListEntity
import com.maple.model.entity.aio.WasteListEntity
import com.maple.utils.store.AioDBHelper
import com.xinchen.aiowaste.base.BaseViewModel
import com.xinchen.aiowaste.model.repository.CommonRepository
import java.util.WeakHashMap


class HomeViewModel: BaseViewModel() {

    private val repository by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository() }

    val userInfo: AioUserInfo? by lazy {
        AioDBHelper.getUser()
    }

    val entId: String by lazy {
        userInfo?.entId?:""
    }

    val devData: MutableLiveData<DevListEntity.DataList> = MutableLiveData()


    fun getDevInfo() {
        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("entId", entId)
        params.put("start", "0")
        params.put("limit", "10")

        launchOnlyResult(
            isShowDialog = false,
            block = {
                repository.devInfo(params)
            }, success = {
                val entity = GsonUtils.fromJson(GsonUtils.toJson(it), DevListEntity::class.java)
                entity.data?.let { data ->
                    if(!data.list.isNullOrEmpty()) {
                        devData.postValue(data.list.first())
                    }
                }
            })
    }

    fun getEnvInfo(scalesNo: String) {
        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("scalesid", scalesNo)
        launchOnlyResult(
            isShowDialog = false,
            block = {
            repository.envInfo(params)
        }, success = {

        })

    }

}